package com.zzmcc.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.zzmcc.jms.common.ModelConfigWms;
import com.zzmcc.jms.common.RouteConfigWms;
import com.zzmcc.stock.StockController;
import com.zzmcc.member.MemberController;
import com.zzmcc.project.ProjectBuildController;
import com.zzmcc.sys.controller.*;
import com.zzmcc.sys.model.*;
import com.zzmcc.tower.TowerController;
import com.zzmcc.weixin.WeixinApiController;
import com.zzmcc.weixin.WeixinMsgController;
import freemarker.template.TemplateModelException;
import net.dreamlu.event.EventPlugin;


import java.util.List;
import java.util.Properties;


/**
 * API引导式配置
 */
public class JFinalWebConfig extends JFinalConfig {
	private Routes routes;
	public Properties loadProp(String pro, String dev) {
		try {return loadPropertyFile(pro);}
		catch (Exception e)
		{return loadPropertyFile(dev);}
	}

	public void configConstant(Constants me) {
		// 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
		loadProp("config.txt", "config_dev.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));

		// ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
		ApiConfigKit.setDevMode(me.getDevMode());

		me.setError401View("/static/html/401.html");//没登录
		me.setError403View("/static/html/403.html");//没权限
		me.setError404View("/static/html/404.html");
		me.setError500View("/static/html/500.html");

		me.setBaseViewPath("/pages/");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		this.routes = me;
		me.add("/jhtml",HtmlController.class);
		me.add("/", IndexController.class);
		me.add("/sys/userinfo", UserinfoController.class);
		me.add("/sys/authorize", RolePermissionController.class);
		me.add("/sys/compare", CompareController.class);
		me.add("/sys/decode", DecodeController.class);
		me.add("/sys/degree", DegreeController.class);
		me.add("/member", MemberController.class);
        me.add("/tower", TowerController.class);
		me.add("/project", ProjectBuildController.class);
		me.add("/stock", StockController.class);
		me.add("/msg", WeixinMsgController.class);
		me.add("/api", WeixinApiController.class, "/api");


		RouteConfigWms.config(me);
		
	}

	public void configPlugin(Plugins me) {
		/* 配置C3p0数据库连接池插件*/

		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);


		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);

		me.add(arp);
		arp.addMapping("sys_userinfo",Userinfo.class);
		arp.addMapping("sys_authorize",Authorize.class);
		arp.addMapping("sys_role_permission",RolePermission.class);
		arp.addMapping("sys_homecounty", HomeCounty.class);
		arp.addMapping("sys_customer", Customer.class);
		arp.addMapping("sys_role", Role.class);
		arp.addMapping("sys_permission", Permission.class);
		arp.addMapping("user",User.class);
		//配置models
		ModelConfigWms.config(arp);

		EhCachePlugin ecp = new EhCachePlugin();
		me.add(ecp);
/*
		// 初始化插件
		EventPlugin eventPlugin = new EventPlugin();
		// 开启全局异步
		eventPlugin.async();
		// 设置扫描jar包，默认不扫描
		eventPlugin.scanJar();
		// 设置监听器默认包，默认全扫描
		eventPlugin.scanPackage("com.zzmcc");
		me.add(eventPlugin);
*/

	}



	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {

		//让freemarker可以使用session
		me.add(new SessionInViewInterceptor());
		//me.add(new PermissionInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler());

		me.add(new HtmlHandler());
	}


	public void afterJFinalStart(){
		try {
			List<HomeCounty> homeCountyList =HomeCounty.dao.find("select * from sys_homecounty");
			List<Customer> customerList =Customer.dao.find("select * from sys_customer");
			List<Role> roleList =Role.dao.find("select * from sys_role");
			FreeMarkerRender.getConfiguration().setSharedVariable("title","漳州铁塔信息管理系统");
			FreeMarkerRender.getConfiguration().setSharedVariable("homeCountyList",homeCountyList);
			FreeMarkerRender.getConfiguration().setSharedVariable("customerList",customerList);
			FreeMarkerRender.getConfiguration().setSharedVariable("roleList",roleList);

		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
	}
}
